package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @Description 《java编程思想》第22章 示例19：
 * <p>
 * 菜单
 **/
public class Menus extends JFrame {
    private String[] flavors = {"Chocolate", "Strawberry",
            "Vanilla", "Mint Chip", "Mocha Almond Fudge", "Rum Raisin", "Mud Pie"};

    private JTextField textField = new JTextField("No flavor", 30);
    private JMenuBar mb1 = new JMenuBar();

    private JMenu f = new JMenu("File");
    private JMenu m = new JMenu("Flavors");
    private JMenu s = new JMenu("Safety");

    private JCheckBoxMenuItem[] safety = {new JCheckBoxMenuItem("Guard"), new JCheckBoxMenuItem("Hide")};
    private JMenuItem[] files = {new JMenuItem("Open")};

    private JMenuBar mb2 = new JMenuBar();
    private JMenu fooBar = new JMenu("fooBar");
    private JMenuItem[] other = {new JMenuItem("Foo", KeyEvent.VK_F),
            new JMenuItem("Bar", KeyEvent.VK_A), new JMenuItem("Baz")};

    private JButton button = new JButton("Swap Menus");

    public Menus(){
        ML ml = new ML();
        CMIL cmil = new CMIL();

        safety[0].setActionCommand("Guard");
        safety[0].setMnemonic(KeyEvent.VK_G);
        safety[0].addItemListener(cmil);
        safety[1].setActionCommand("Hide");
        safety[1].setMnemonic(KeyEvent.VK_H);
        safety[1].addItemListener(cmil);

        other[0].addActionListener(new Fool());
        other[1].addActionListener(new BarL());
        other[2].addActionListener(new BarzL());

        FL fl = new FL();
        int n = 0;
        for(String flavor:flavors) {
            JMenuItem mi = new JMenuItem(flavor);
            mi.addActionListener(fl);
            m.add(mi);

            if ((n++ + 1) %3 == 0)  {
                m.addSeparator();
            }
        }

        for(JCheckBoxMenuItem sfty:safety) {
            s.add(sfty);
        }
        s.setMnemonic(KeyEvent.VK_A);
        f.add(s);
        f.setMnemonic(KeyEvent.VK_F);
        for (int i=0;i< files.length;i++) {
            files[i].addActionListener(ml);
            f.add(files[i]);
        }
        mb1.add(f);
        mb1.add(m);
        setJMenuBar(mb1);
        textField.setEnabled(false);
        add(textField, BorderLayout.CENTER);

        button.addActionListener(new BL());
        button.setMnemonic(KeyEvent.VK_S);
        add(button, BorderLayout.NORTH);
        for(JMenuItem oth:other) {
            fooBar.add(oth);
        }
    }

    public static void main(String[] args) {
        SwingConsole.run(new Menus(), 300, 200);
    }

    class BL implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JMenuBar m = getJMenuBar();
            setJMenuBar(m == mb1 ? mb2 : mb1);
            validate();
        }
    }

    class ML implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JMenuItem target = (JMenuItem) e.getSource();
            String actionCommand = target.getActionCommand();
            if (actionCommand.equals("Open")) {
                String s = textField.getText();
                boolean chosen = false;
                for (String flavor : flavors) {
                    chosen = true;
                }
                if (!chosen) {
                    textField.setText("Choose a flavor first");
                } else {
                    textField.setText("Opening " + s + ". Mmm. mm!");
                }
            }
        }
    }

    class FL implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JMenuItem target = (JMenuItem) e.getSource();
            textField.setText(target.getText());
        }
    }

    class Fool implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText("Foo selected");
        }
    }

    class BarL implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText("Bar selected");
        }
    }

    class BarzL implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText("Bar selected");
        }
    }

    class CMIL implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            JCheckBoxMenuItem target = (JCheckBoxMenuItem) e.getItem();

            String actionCommand = target.getActionCommand();
            if (actionCommand.equals("Guard")) {
                textField.setText("Guard the Ice Cream");
            } else if (actionCommand.equals("Hide")) {
                textField.setText("Guard the Ice Cream!" + "Guarding is " +
                        target.getState());
            }else if(actionCommand.equals("Hide")) {
                textField.setText("Hide the Ice Cream!" + "Is is hidden? " +
                        target.getState());
            }
        }
    }


}
